package com.cms.edm.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cms.common.utils.R;
import com.cms.edm.dao.CourseDao;
import com.cms.edm.entity.CourseEntity;
import com.cms.edm.entity.vo.TemplateVo;
import com.cms.edm.feign.StuFeign;
import com.cms.edm.to.MajorTo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cms.common.utils.PageUtils;
import com.cms.common.utils.Query;

import com.cms.edm.dao.TemplateDao;
import com.cms.edm.entity.TemplateEntity;
import com.cms.edm.service.TemplateService;

import javax.annotation.Resource;


@Service("templateService")
public class TemplateServiceImpl extends ServiceImpl<TemplateDao, TemplateEntity> implements TemplateService {

    @Resource
    private CourseDao courseDao;

    @Autowired
    private StuFeign stuFeign;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<TemplateEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            wrapper.like("id", key);
        }
        IPage<TemplateEntity> page = this.page(
                new Query<TemplateEntity>().getPage(params),
                wrapper
        );

        List<TemplateEntity> records = page.getRecords();
        List<TemplateVo> result = records.stream().map(item -> {
            TemplateVo templateVo = new TemplateVo();
            BeanUtils.copyProperties(item, templateVo);
            StringBuffer stringBuffer = new StringBuffer();
            for (String s : item.getCourses().split(",")) {
                CourseEntity courseEntity = courseDao.selectById(Long.parseLong(s));
                stringBuffer.append(courseEntity.getName() + "、");
            }
            String s = stringBuffer.toString();
            templateVo.setCourseNames(s.substring(0, s.length() - 1));

            R r = stuFeign.info(item.getMajorId());
            if (r.getCode() == 0) {
                templateVo.setMajorName(r.getData("major", new TypeReference<MajorTo>() {
                }).getName());
            }
            return templateVo;
        }).collect(Collectors.toList());

        IPage<TemplateVo> resultPage = new Page<>();
        resultPage.setRecords(result);
        resultPage.setCurrent(page.getCurrent());
        resultPage.setCurrent(page.getCurrent());
        resultPage.setPages(page.getPages());
        resultPage.setSize(page.getSize());
        resultPage.setTotal(page.getTotal());

        return new PageUtils(resultPage);
    }

}